<template>
  <div>
    <el-button type="primary" @click="addfn">增加</el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="日期" width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.date }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" width="180">
        <template slot-scope="scope">
          <el-popover placement="top">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.name }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="住址" width="220">
        <template slot-scope="scope">
          <el-popover placement="top">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.address }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>

      <!-- 弹窗表单 -->
      <el-dialog
        :title="iseid"
        :visible.sync="dialogFormVisible"
        :append-to-body="true"
      >
        <el-form :model="form">
          <el-form-item label="日期" :label-width="formLabelWidth">
            <el-input v-model="form.date" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="姓名" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="住址" :label-width="formLabelWidth">
            <el-input v-model="form.address" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click= "submitfn"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        // 表单里的内容
        name: "",
        address: "",
        date: "",
      },
      formLabelWidth: "120px",
       iseid:"新增",
       editIndex:0
    };
  },
  mounted() {
    this.$axios.get("/list").then((res) => {
      this.tableData = res.data;
    });
  },
  methods: {
    handleEdit(index, row) {
      this.iseid = "编辑"
      console.log(index, row);
      this.dialogFormVisible = true;
      console.log(row,11111);
      // let aaa = JSON.parse(JSON.stringify(row));
      // console.log(aaa);
      this.editIndex = index
      this.form = JSON.parse(JSON.stringify(row)); // form不会影响表格变化了
      // console.log(form);
    },
    handleDelete(index, row) {
      console.log(index, row);

  
    },
    addfn(){
         this.iseid = "新增"
         this.dialogFormVisible = true;
         this.form = {}
    },
    submitfn(){
       if (this.iseid == "新增") {
           console.log(this.form);
           this.tableData.push(this.form)
       } else {
            console.log(this.form);
            this.tableData.splice(this.editIndex,1,this.form)  
       }
        this.dialogFormVisible = false;
    }
  },
};
</script>

<style>
</style>
